using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Evaluation3.utils;
using Evaluation3.data;
using System.Data.OleDb;
public partial class product_ticket : System.Web.UI.Page
{
    private String firstStep = "0";
    private String maxStep = "5";

    private decimal totalMax_ = 1;
    private decimal totalMin_ = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            Session[keyUtils.SES_SELECTEDTAB] = "0";
            diemsanphamView.DataSource = getProductTicketData();
            diemsanphamView.DataBind();

        }
            String tabNumber = (String)Session[keyUtils.SES_SELECTEDTAB];
            
            if(tabNumber == "1")
            {
                PhieuDiemTheoCongDoanSX_GrdView.DataSource = getDulieuDiemSanphamTheoCongDoanSX();
                PhieuDiemTheoCongDoanSX_GrdView.DataBind();
            }
                

    

        diemsanphamGridInfo.Visible = (diemsanphamView.Rows.Count == 0);
        diemsanphamGridTotal.Visible = (diemsanphamView.Rows.Count > 0);
             
    }
    protected String getSelectedTab() { return Session[keyUtils.SES_SELECTEDTAB].ToString(); }
    protected void setSelectedTab(String i) { Session[keyUtils.SES_SELECTEDTAB] = i; }
    protected void ChangeTabIndex(object sender, EventArgs e)
    {
        String senderId = ((LinkButton)sender).ID;
        setSelectedTab(senderId.Substring(senderId.Length - 1));
    }
    public decimal Total(ControlCollection controls)
    {
        decimal d = 0;
        foreach (Control c in controls)
        {
            decimal value = Convert.ToDecimal(((TextBox)c.Controls[3]).Text);
            d = d + value;

        }
        return d;
    }
    public static bool IsEMPTY(String values)
    {
        return (values == null || values.Trim().Length == 0);
    }
    public void ErrorMessage(String message)
    {
        Controls.Add(new LiteralControl(MessageBox.SHOW(message, MessageBox.INFO)));
    }
    public void setRenderControl(int i)
    {
    }
    protected void resetValue(object sender, EventArgs e)
    {
        diemsanphamViewRefresch();
    }
    public void diemsanphamViewRefresch(){
    diemsanphamView.DataSource = getProductTicketData();
        diemsanphamView.DataBind();
    }
    public void viewStep(int step)
    {
    }
    protected void backStep(object sender, EventArgs e)
    {
        int tab = Convert.ToInt32(getSelectedTab());
        if (tab > 0) tab--;
        setSelectedTab(tab.ToString());
    }
    protected void nextStep(object sender, EventArgs e)
    {
        int tab = Convert.ToInt32(getSelectedTab());
        if (tab < 2) tab++;
        setSelectedTab(tab.ToString());
    }
    protected void saveWizard(object sender, EventArgs e)
    {
    }

    protected void cancelWizard(object sender, EventArgs e)
    {
        Session[keyUtils.SES_SELECTEDTAB] = "0";
        Response.Redirect("default.aspx");

    }

    protected void changeCucil(object sender, EventArgs e)
    {
        sanphamData.SelectParameters.UpdateValues(this.Context, cb_hoidong);
        diemsanphamViewRefresch();
    }

    protected void changeProduct(object sender, EventArgs e)
    {
        diemsanphamViewRefresch();
    }

    protected void changeMember(object sender, EventArgs e)
    {
        diemsanphamViewRefresch();
    }

    protected DataView getProductTicketData()
    {
        String slq1 = "select * from hesotrongluong where mahoidong=@mahoidong";
        String sql2 = "select * from diemsanpham  where mahesotrongluong_sanpham=@mahesotrongluongSanpam and MaThanhVien=@mathanhvien  and MaSanPham=@masapham ";
        
        String tableName = "ProductTicketData";
        String idField = "maso";
        String noidungField = "noidung";
        String diemField = "diem";
        String diemgocField = "diemgoc";
        String tongsoField = "tongso";

        DataTable db = new DataTable(tableName);
        db.Columns.Add(new DataColumn(idField));

        db.Columns.Add(new DataColumn(noidungField));
        db.Columns.Add(new DataColumn(diemField));
        db.Columns.Add(new DataColumn(diemgocField));
        db.Columns.Add(new DataColumn(tongsoField));
        DataRow dr;
        HeSoTrongLuong objHesotrongluong = new HeSoTrongLuong();
        objHesotrongluong.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
         objHesotrongluong.SelectCondition = "mahoidong='"+cb_hoidong.SelectedValue+"'";
         //objHesotrongluong.SelectCondition = "mahoidong= 40";
        objHesotrongluong.LoadData();
        foreach (DataRow r in objHesotrongluong.Rows)
        {

            DiemSanPham objDiemSanpham = new DiemSanPham();
            objDiemSanpham.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
             objDiemSanpham.SelectCondition = "mahesotrongluong_sanpham ='" + r[HeSoTrongLuong.MaHeSoTrongLuong_SanPham] + "' and mathanhvien='"+cb_thanhvien.SelectedValue+"' and masanpham='"+cb_sanpham.SelectedValue+"'" ;
            //objDiemSanpham.SelectCondition = "mahesotrongluong_sanpham =1 and mathanhvien=47 and masanpham= 51";
            objDiemSanpham.LoadData();
            dr = db.NewRow();
            dr[idField] = r[HeSoTrongLuong.MaHeSoTrongLuong_SanPham];
            dr[noidungField] = r[HeSoTrongLuong.TenHeSoTrongLuong];
            dr[diemField] = r[HeSoTrongLuong.Diem];

            foreach (DataRow rv in objDiemSanpham.Rows)
            {
               
               
                dr[diemgocField] = rv[DiemSanPham.DiemGoc];
                dr[tongsoField] = Convert.ToDecimal(r[HeSoTrongLuong.Diem]) * Convert.ToDecimal(rv[DiemSanPham.DiemGoc]);
                db.Rows.Add(dr);

            }
        }
        return db.DefaultView;
    }

    protected DataView getDulieuDiemSanphamTheoCongDoanSX()
    {
        String maDN = (String)Session["companyId"];
        String maSP = cb_sanpham.DataValueField;
        String maHD = cb_hoidong.DataValueField;
        String maTV = cb_thanhvien.DataValueField;

        //String sql1 = "SELECT BangChiTieu.MaChiTieu, TenChiTieu,GhiChu FROM BangChiTieu inner join ChiTietChiTieu on BangChiTieu.MaChiTieu=ChiTietChiTieu.MaChiTieu WHERE MaDoanhNghiep=" + maDN ;               

        DataRow dr;
        BangChiTieu objBangChiTieu;
        BuocGiaCong objBuocGiaCong;
        DiemChiTieu_KyThuat objDiemChiTieuKyThuat;

        String tableName = "PhieuDiemTheoCongDoanSX";
        String idField = "maso";
        String chitieuKTField = "cacchitieuKT";
        String diemMAXField = "diemMAX";
        String diemMINField = "diemMIN";
        String tieuchuanField = "Tieuchuandiem";
        String TenBuocGiaCongField = "TenBuocGiaCong";
        DataTable db = new DataTable(tableName);
        db.Columns.Add(new DataColumn(idField));
        db.Columns.Add(new DataColumn(chitieuKTField));
        db.Columns.Add(new DataColumn(diemMAXField));
        db.Columns.Add(new DataColumn(diemMINField));
        db.Columns.Add(new DataColumn(tieuchuanField));
               
        //OleDbConnection conn = dataUtils.GETOLEDBCONNECTION();
        //OleDbDataAdapter chitieu_adapter = new OleDbDataAdapter(sql1,conn);
        //DataSet ds_chitieu = new DataSet();
        //chitieu_adapter.Fill(ds_chitieu,"BangChiTieu");
        //DataTable dataTable= ds_chitieu.Tables.Add("BangChiTieu");
        //objBangChiTieu = dataTable;
        objBangChiTieu = new BangChiTieu();
        objBangChiTieu.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
        //objBangChiTieu.SelectCommand = "SELECT BangChiTieu.MaChiTieu, TenChiTieu,GhiChu,MaDoanhNghiep FROM BangChiTieu inner join ChiTietChiTieu on BangChiTieu.MaChiTieu=ChiTietChiTieu.MaChiTieu";
        objBangChiTieu.SelectCondition = " MaDoanhNghiep=" + maDN;
        objBangChiTieu.LoadData();

        foreach(DataRow r in objBangChiTieu.Rows){
            dr = db.NewRow();
            dr[idField] = r[BangChiTieu.MaChiTieu];
            dr[chitieuKTField] = r[BangChiTieu.TenChiTieu];

            
             
            objBuocGiaCong = new BuocGiaCong();
            objBuocGiaCong.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
            objBuocGiaCong.SelectCondition = " MaSanPham=" + maSP +  " AND MaHoiDong=" + maHD;
            objBuocGiaCong.LoadData();

            foreach (DataRow drDiemChiTieuKyThuat in objBuocGiaCong.Rows)
            {
                objDiemChiTieuKyThuat = new DiemChiTieu_KyThuat();
                objDiemChiTieuKyThuat.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
                objDiemChiTieuKyThuat.SelectCondition = " MaThanhVien=" + maTV + " and MaBuocGiaCong=" + drDiemChiTieuKyThuat[BuocGiaCong.MaBuocGiaCong] + " and MaChiTieu=" + r[BangChiTieu.MaChiTieu];
                objDiemChiTieuKyThuat.LoadData();
                foreach (DataRow drMaxMin in objDiemChiTieuKyThuat.Rows) {
                    dr[diemMAXField] = drMaxMin[DiemChiTieu_KyThuat.Tamax];
                    dr[diemMINField] = drMaxMin[DiemChiTieu_KyThuat.Tamin];    
                }
               // dr[TenBuocGiaCongField] = drDiemChiTieuKyThuat[BuocGiaCong.TenBuocGiaCong];
            }
            dr[tieuchuanField] = r[BangChiTieu.GhiChu];
            db.Rows.Add(dr);
        }       
        return db.DefaultView;
    }

    protected DataView getProductRestData()
    {
        String slq1 = "select * from hesotrongluong where mahoidong=@mahoidong";
        String sql2 = "select * from diemsanpham  where mahesotrongluong_sanpham=@mahesotrongluongSanpam and MaThanhVien=@mathanhvien  and MaSanPham=@masapham ";

        String tableName = "ProductTicketData";
        String idField = "maso";
        String noidungField = "noidung";
        String diemField = "diem";
        String diemgocField = "diemgoc";
        String tongsoField = "tongso";

        DataTable db = new DataTable(tableName);
        db.Columns.Add(new DataColumn(idField));

        db.Columns.Add(new DataColumn(noidungField));
        db.Columns.Add(new DataColumn(diemField));
        db.Columns.Add(new DataColumn(diemgocField));
        db.Columns.Add(new DataColumn(tongsoField));
        DataRow dr;
        HeSoTrongLuong objHesotrongluong = new HeSoTrongLuong();
        objHesotrongluong.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
        objHesotrongluong.SelectCondition = "mahoidong='" + cb_hoidong.SelectedValue + "'";
        //objHesotrongluong.SelectCondition = "mahoidong= 40";
        objHesotrongluong.LoadData();
        foreach (DataRow r in objHesotrongluong.Rows)
        {

            DiemSanPham objDiemSanpham = new DiemSanPham();
            objDiemSanpham.ActiveConnection = dataUtils.GETOLEDBCONNECTION();
            //objDiemSanpham.SelectCondition = "mahesotrongluong_sanpham ='" + r[HeSoTrongLuong.MaHeSoTrongLuong_SanPham] + "' and mathanhvien='"+cb_thanhvien.SelectedValue+"' and masanpham='"+cb_sanpham.SelectedValue+"'" ;
            objDiemSanpham.SelectCondition = "mahesotrongluong_sanpham =1 and mathanhvien=47 and masanpham= 51";
            objDiemSanpham.LoadData();
            dr = db.NewRow();
            dr[idField] = r[HeSoTrongLuong.MaHeSoTrongLuong_SanPham];
            dr[noidungField] = r[HeSoTrongLuong.TenHeSoTrongLuong];
            dr[diemField] = r[HeSoTrongLuong.Diem];

            foreach (DataRow rv in objDiemSanpham.Rows)
            {


                dr[diemgocField] = rv[DiemSanPham.DiemGoc];
                dr[tongsoField] = Convert.ToDecimal(r[HeSoTrongLuong.Diem]) * Convert.ToDecimal(rv[DiemSanPham.DiemGoc]);
                db.Rows.Add(dr);

            }
        }
        return db.DefaultView;
    }

    protected void cb_toolDataViewChanged(object sender, EventArgs e) { 
    }

    protected void saveDiemSanpham() { 
    }

    protected void saveDiemGiaTriconlai() { }

    protected void saveDiemSanPhamTheoCongDoanSX() { 
    
    }
}
